*
* Purpose: Prepare ROL characteristics
*

* TODO: define which variables to use
use szakkod eljaras karkod szaknev szint munkarend fin_form field_study szekhely kepztelep using "${institutions_raw}", clear

drop if szakkod == 17943 | szakkod == 17944

keep eljaras karkod szaknev szint munkarend fin_form field_study szekhely kepztelep
* TODO define which variables to use
merge 1:n eljaras karkod szaknev szint munkarend fin_form field_study using "${application_raw}"

keep if eljaras == 1 & _m == 3
drop _m

rename szint program_level

do "${dir_code_prepare_data}/helpers/addCapacityClass.do"

rename jelsorsz rank
rename osszjel rollength
label variable rollength "Contracts on ROL (admin data)"
rename fin_form funding
rename munkarend schedule
rename karkod faculty
rename szaknev major
label variable capacity_class "Capacity class"

drop field_study Stem ev eljaras jelsorsz0

* Create contract id and program id --------------------------------------------
sort faculty major program_level schedule capacity_class funding
bys faculty major program_level schedule capacity_class funding: gen ss1 = _n
replace ss1 = . if ss1 > 1
sort ss1 faculty major program_level schedule capacity_class funding
bys ss1: gen contract_id_temp = _n
replace contract_id_temp = . if ss1 ! =1
bys faculty major program_level schedule capacity_class funding: egen contract_id = min(contract_id_temp)

bys faculty major program_level schedule capacity_class: egen program_id = min(contract_id)
label variable program_id "Program id"
label variable contract_id "Contract id"
drop ss1 contract_id_temp

do "${dir_code_prepare_data}/helpers/addUniversity.do"

encode university, gen(uni)
drop university
rename uni university

sort id rank
order id rank contract_id program_id university faculty major program_level schedule funding

encode kepztelep, gen(program_location)
drop kepztelep
label variable program_location "Location of program"

* ------------------------------------------------------------------------------
* ROL-specific variables -------------------------------------------------------
* ------------------------------------------------------------------------------

* Ranked funded contract only --------------------------------------------------
bys id: egen funded_min = min(funding)
bys id: egen funded_max = max(funding)

gen funded_only = funded_max == 1
label var funded_only "State-funded contract exclusively"
gen unfunded_only = funded_min == 2
label var unfunded_only "Self-funded contract exclusively"
cap drop funded_min funded_max

* Market power -----------------------------------------------------------------
sort id rank
bys id: gen program_id_next = program_id[_n + 1]

gen mp_temp = program_id == program_id_next & funding == 1
bys id: egen mp = max(mp_temp)
label variable mp "Market power"

gen mp_temp_1 = program_id == program_id_next & funding == 1 & rank == 1
bys id: egen mp1 = max(mp_temp_1)
label variable mp1 "Market power at the highest-ranked contract"
cap drop *_temp* *_next

* Preference for university ----------------------------------------------------
bys id: egen university_min_temp = min(university)
bys id: egen university_max_temp = max(university)
gen pref_for_uni = university_max_temp == university_min_temp
label variable pref_for_uni "Single university"
drop *_temp

* Preference for faculty -------------------------------------------------------
encode faculty, gen(faculty2)
drop faculty
rename faculty2 faculty
bys id: egen min_temp = min(faculty)
bys id: egen max_temp = max(faculty)
gen pref_for_faculty = max_temp == min_temp
label variable pref_for_faculty "Single faculty"
drop *_temp

* Preference for field ---------------------------------------------------------
bys id: egen min_temp = min(capacity_class)
bys id: egen max_temp = max(capacity_class)
gen pref_for_field = max_temp == min_temp
label variable pref_for_field "Single field of study"
drop *_temp

* Preference for major ---------------------------------------------------------
encode major, gen(major2)
drop major
rename major2 major
bys id: egen min_temp = min(major)
bys id: egen max_temp = max(major)
gen pref_for_major = max_temp == min_temp
label variable pref_for_major "Single major"
drop *_temp

* Preference for location ------------------------------------------------------
bys id: egen min_temp = min(program_location)
bys id: egen max_temp = max(program_location)
gen pref_for_location = max_temp == min_temp
label variable pref_for_location "Single program location"
drop *_temp

* TODO: define which variables to keep
*keep if rank == 1
*keep id rank mp mp1 pref_for_location pref_for_uni pref_for_faculty ///
	pref_for_field pref_for_major funded_only unfunded_only

compress
save "${data_rols}", replace